#include "headfiles.h"

using namespace std;

int longestNoRepitition(const string &s, int &start, int &end) {
    bool *flag = new bool[256];
    if (flag == NULL) {
        cout << "Allocate flag buffer error!" << endl;
        return -1;
    }

    memset(flag, 0, sizeof(bool) * 256);
    int size = s.size();
    int max_len = 0;
    int i, j;
    const char *str = s.c_str();
    j = 0;
    for (i = 0; i < size; i++) {
        // This char already exists
        if (flag[str[i]]) {
            while (j < i) {
                flag[str[j]] = false;
                if (str[j] == str[i]) {
                    j++;
                    break;
                }
                j++;
            }
        }
        else {
            if (max_len < i - j + 1) {
                max_len = i - j + 1;
                start = j;
                end = i;
            }
        }

        flag[str[i]] = true;
    }

    delete flag;

    return max_len;
}

